<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Escort_message_thread_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $origin = '';
	var $owner = '';
	var $solver_permission_slug = '';
	var $created_time = 0;
	var $status = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setOrigin($input){
		$this->origin = $input;
	}
	function setOwner($input){
		$this->owner = $input;
	}
	function setSolverPermissionSlug($input){
		$this->solver_permission_slug = $input;
	}
	function setCreatedTime($input){
		$this->created_time = $input;
	}
	function setStatus($input){
		$this->status = $input;
	}
	function getId(){
		return $this->id;
	}
	function getOrigin(){
		return $this->origin;
	}
	function getOwner(){
		return $this->owner;
	}
	function getSolverPermissionSlug(){
		return $this->solver_permission_slug;
	}
	function getCreatedTime(){
		return $this->created_time;
	}
	function getStatus(){
		return $this->status;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_MESSAGE_THREAD)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->origin = $result[0]->origin;
			$this->owner = $result[0]->owner;
			$this->solver_permission_slug = $result[0]->solver_permission_slug;
			$this->created_time = $result[0]->created_time;
			$this->status = $result[0]->status;
			return $this;
		}
		return false;
	}
	
	function getMessages($solver_permission_slug, $status){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('solver_permission_slug',$solver_permission_slug);
		$ci->db->where('status',$status);
		return $ci->db->get(TBL_MESSAGE_THREAD)->result();
	}
		
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('origin',$this->getOrigin());
		$ci->db->set('owner',$this->getOwner());
		$ci->db->set('solver_permission_slug',$this->getSolverPermissionSlug());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('status',$this->getStatus());
		$ci->db->insert(TBL_MESSAGE_THREAD);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('origin',$this->getOrigin());
		$ci->db->set('owner',$this->getOwner());
		$ci->db->set('solver_permission_slug',$this->getSolverPermissionSlug());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('status',$this->getStatus());
		return $ci->db->where('id', $this->id)->update(TBL_MESSAGE_THREAD);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_MESSAGE_THREAD);
	}
	
	///////////////
	
	function listAllThreadMessages($escort_id, $user_type){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->where(array("escort_id"=>$escort_id, "origin"=>$user_type))->order_by('created_time','desc')->get(TBL_MESSAGE_THREAD)->result();
	}
	
	function listAllMessagesOfThread($thread_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->where("thread_id = $thread_id AND is_deleted = 0")->order_by('sent_time','asc')->get(TBL_MESSAGE)->result();
	}
	
	function countAllUnReadMessage($thread_id){
		$allMessages = $this->listAllMessagesOfThread($thread_id);
		$count = 0;
		foreach($allMessages as $item){
			if($item->read != 1){
				$count ++;
			}
		}
		return $count;
	}
	
	function countAllUnreadMessagesOfUser($escort_id, $user_type){
		$allThreads = $this->listAllThreadMessages($escort_id, $user_type);
		$count = 0;
		foreach($allThreads as $item){
			$count += $this->countAllUnReadMessage($item->id);
		}
		return $count;
	}
	
	function getFeatureMessageOfThread($thread_id){
		$allMessages = $this->listAllMessagesOfThread($thread_id);
		return $allMessages ? $allMessages[0] : false;
	}
	
	function getMessageObject($message_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$rs = $ci->db->where('id',$message_id)->get(TBL_MESSAGE)->result();
		return ($rs) ? $rs[0] : false;
	}
	
	function getMessageThreadByEscortIdList($escortIdList, $usergroup, $theadIdList = array(), $limit = array()){
		$ci = & get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where_in('escort_id', $escortIdList);
		if($theadIdList && count($theadIdList) > 0){
			$ci->db->where_in('id', $theadIdList);
		}
		if($usergroup == USER_ADMIN || $usergroup == USER_COUNTRY_MANAGER){
			$ci->db->where('status', 0);
			$ci->db->or_where('solver_permission_slug', $usergroup);
		}else{
			$ci->db->where('solver_permission_slug', $usergroup);
		}
		
		if($limit && count($limit) > 0){
			$ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('created_time','desc');
		$result = $ci->db->get(TBL_MESSAGE_THREAD)->result();
		return $result;
	}
	function countMessageThreadByEscortIdList($escortIdList, $usergroup, $theadIdList = array(), $unread = false){
		$ci = & get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where_in('escort_id', $escortIdList);
		if($usergroup == USER_ADMIN || $usergroup == USER_COUNTRY_MANAGER){
			$ci->db->where('status', 0);
			$ci->db->or_where('solver_permission_slug', $usergroup);
		}else{
			$ci->db->where('solver_permission_slug', $usergroup);
		}
		if($theadIdList && count($theadIdList) > 0){
			$ci->db->where_in('id', $theadIdList);
		}
		if($unread){
			$ci->db->where('status', 0);
		}
		$result = $ci->db->count_all_results(TBL_MESSAGE_THREAD);
		return $result;
	}
	function displayUnreadMessage($thread_id){
		$unreadMessages = $this->countAllUnReadMessage($thread_id);
	 	if($unreadMessages<1){
	 		return '';
	 	}else{
	 		//$str = "&#60;span id='unread_thread_$thread_id' class='unread-thread'&#62;( ".$unreadMessages.'&#60;&#47;span&#62; '.language_translate('support_new_unread_messages').' )';
	 		//$str = strip_tags($str);
	 		$str = "( $unreadMessages ".language_translate('support_new_unread_messages')." )";
	 		return $str;//htmlentities($str,ENT_COMPAT,'UTF-8',false);
		}
	}
	
	function updateReadStatus($thread_id){
		$ci = & get_instance();
		$ci->db->set_dbprefix('default_');
		
		$listAllMessages = $this->listAllMessagesOfThread($thread_id);
		foreach($listAllMessages as $item){
			if($item->read != 1){
				$ci->db->set('read',1);
				$ci->db->where('id',$item->id)->update(TBL_MESSAGE);
			}
		}	
	}
	//endclass
}